import {Component, Input, OnInit} from '@angular/core';
import {MockConditionItem} from '../../entity/mock-condition-item';

@Component({
    selector: 'app-doc-mock-condition-table',
    templateUrl: './doc-mock-condition-table.component.html',
    styleUrls: ['./doc-mock-condition-table.component.css']
})
export class DocMockConditionTableComponent implements OnInit {
    @Input()
    params: MockConditionItem[];

    constructor() {
    }

    ngOnInit() {
    }

    onKeyChange(kv: MockConditionItem) {
        if (kv.key.trim() === '') {
            return;
        }
        // 检查是否最后一个是空值
        let hasEmptyItem = false;
        for (let i = 0; i < this.params.length; i++) {
            const param = this.params[i];
            if (param.key === '') {
                hasEmptyItem = true;
                break;
            }
        }
        if (!hasEmptyItem) {
            this.params.push(<MockConditionItem>{
                key: '',
                value: '',
                operator: '==',
                available: false
            });
        }
    }

    deleteItem(index: number) {
        if (this.params.length <= index) {
            return;
        }
        if (this.params.length === 1) {
            return;
        }
        this.params.splice(index, 1);
    }
}
